#!/usr/bin/python
#
# Copyright 2010 Google Inc. All Rights Reserved.

from bcc import *

import unittest


class TestParser(unittest.TestCase):
  """Unit tests for the Breadcrumb parser."""

  def setUp(self):
    self.parser = Parser()

  def check(self, story, page_count):
    out = self.parser.Parse(story)
    self.assert_(out is not None, story)
    self.assertEqual(len(out), page_count,
                     'generated %i pages instead of %i: %r' %
                     (len(out), page_count, out))
    return out

  def checkPage(self, page, id, _value):
    self.assertTrue('id' in page, 'page %r has no id' % page)
    self.assertTrue('_value' in page, 'page %r has no _value' % page)
    self.assertEqual(page['id'], id, 'page %r has the wrong id %r' % (page, id))
    self.assertTrue(_value in page['_value'],
                    'page %r has _value not including %r' % (page, _value))

  def testSimple(self):
    out = self.check('(1) Hello, world!', 1)
    self.checkPage(out[0], '1', 'Hello, world!')

  def testTwoPages(self):
    out = self.check("""
                     (1) Hello, world! [Continue](2)?
                     (2) Hello again, world!
                     """, 2)
    self.checkPage(out[0], '1', 'Hello, world!')
    self.checkPage(out[0], '1', '<a')
    self.checkPage(out[1], '2', 'Hello again, world!')

  def testTwoPagesWithHtmlInLink(self):
    out = self.check("""
                     (1) Hello, world! [<b>Continue</b>](2)?
                     (2) Hello again, world!
                     """, 2)
    self.checkPage(out[0], '1', 'Hello, world!')
    self.checkPage(out[0], '1', '<b>Continue</b>')

  def testTwoPagesWithBadHtmlInLink(self):
    out = self.check("""
                     (1) Hello, world! [<button>Continue](2)?
                     (2) Hello again, world!
                     """, 2)
    self.checkPage(out[0], '1', 'Hello, world!')
    self.checkPage(out[0], '1', '&lt;button&gt;Continue')

  def testMarkdown(self):
    out = self.check("""
                     (1) Hello, world, with EOLs!

                     [Continue](2)?

                     (2) Hello *again*, world!
                     """, 2)
    self.checkPage(out[0], '1', "EOLs!</p>")
    self.checkPage(out[1], '2', '<em>again</em>')

  def testEOLThenNewPage(self):
    out = self.check("""
                     (1) Hello, world, with EOLs; [Continue](2)

                     (2) Hello *again*, world!
                     """, 2)
    self.checkPage(out[0], '1', ">Continue</a>")
    self.checkPage(out[1], '2', '<em>again</em>')

  def testAttachedScripts(self):
    out = self.check("""
                     (1) Hello, world! [Continue](2: back='1')?
                     (2: alert('Hi!')) Hello again, world!
                     """, 2)
    self.checkPage(out[0], '1', 'Hello, world!')
    self.checkPage(out[1], '2', 'Hello again, world!')

  def testEmbeddedScripts(self):
    out = self.check("""
                     (1) Hello, <%= 'green' %> world! [Continue](2)?
                     (2) Hello again, world! <% alert('Hi!') %>
                     """, 2)
    self.checkPage(out[0], '1', "p.push('green')")
    self.checkPage(out[1], '2', 'alert(')

  def testLinkAfterEndOfLine(self):
    out = self.check("""
(1) Test

Test [link] (2).

(2) Test 2.
                     """, 2)
    self.checkPage(out[0], '1', '<a')
    self.checkPage(out[1], '2', 'Test 2')

  def testSimpleScript(self):
    out = self.check('(1: baz=[foo]) Hello, world!', 1)
    self.checkPage(out[0], '1', 'baz=[foo]')
    self.checkPage(out[0], '1', 'Hello, world!')

  def testScriptWithoutEOLs(self):
    out = self.check("""
(1) Testing

Hello, <%= cruel %> world!<br/>
Line Two.
                     """, 1)
    self.checkPage(out[0], '1', ');p.push(cruel);p.push(')

  def testInterestingScript(self):
    out = self.check("""
                     (1) Hello, [world](2: foo.happy-=.25)
                     (2) Is <%= foo.happy %> < 0?
                     """, 2)
    self.checkPage(out[0], '1', 'foo.happy-=.25')

  def testExternalLink(self):
    out = self.check("""
                     (1) Hello, [world](http://google.com/)
                     """, 1)
    self.checkPage(out[0], '1', '<a href=')

  def testSyntaxNotServerError(self):
    try:
      out = self.check('(1) Hello, [world]! (http://google.com/)', 1)
      self.checkPage(out[0], 'error', 'world')
    except:
      self.assertFalse()


if __name__ == '__main__':
  unittest.main()
